package server.commonData;

import java.util.concurrent.atomic.AtomicLong;

/**
 * id工厂
 * 区id+服务器+时间+（id++）
 * 7位+8位+32位+16位
 * @author King
 *
 */
public class IdFactory
{

	private static final AtomicLong idIncrement = new AtomicLong();

	
	/**区id**/
	private static int zid;
	/**服务器id**/
	private static int sid;
	/**上次玩家id生成时间**/
	private static int lastUserCreateTime;
	/**玩家id自增**/
	private static long userIdAuto= 0;
	
	public static void init(int zid,int sid)
	{
		IdFactory.zid = zid;
		IdFactory.sid = sid;
	}
	
	/**
	 * 获得一个桌子id
	 * @return
	 */
	public static final long getTableId()
	{
		return idIncrement.addAndGet(1);
	}
	
	/**创建用户id  每秒生成65536个id**/
	public static final long createUserId()
	{
		int second = Utils.currentTimeSecond();
		if (second == lastUserCreateTime)
		{
			userIdAuto++;
			long seedMax = (1 << 16);// 每秒支持65536个id的生成
			if (userIdAuto > seedMax)
			{
				userIdAuto = 0;
			}
		}
		else
		{
			lastUserCreateTime= second;
			userIdAuto = 0;
		}
		return (zid*1L<<56)+(sid*1L<<48)+(Utils.currentTimeSecond()*1L<<16)+userIdAuto;
	}
	
	
}
